package qmm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import qmm.dao.BaseOrderDao;
import qmm.dao.CartDao;
import qmm.dao.FundDao;
import qmm.dao.GoodsDao;
import qmm.dao.OrderInfoDao;
import qmm.dao.impl.BaseOrderDaoImpl;
import qmm.dao.impl.CartDaoImpl;
import qmm.dao.impl.FundDaoImpl;
import qmm.dao.impl.GoodsDaoImpl;
import qmm.dao.impl.OrderInfoDaoImpl;
import qmm.entity.BaseOrder;
import qmm.entity.OrderInfo;
import qmm.entity.User;
import qmm.entity.vo.CartVo;
import qmm.entity.vo.OrderVo;

@WebServlet("/BaseOrderServlet")
public class BaseOrderServlet  extends BaseServlet{
	BaseOrderDao dao = new BaseOrderDaoImpl();
	
	//添加订单：1、添加订单信息；2、添加订单详情；3、删除购物车中订单对应的商品；4、刷新购物车商品；5、更新库存；6、余额支付
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
		CartDao cartDao = new CartDaoImpl();
		FundDao fundDao = new FundDaoImpl();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		
		
		int addressId = Integer.parseInt(request.getParameter("addressId"));//地址
		double money = (double) session.getAttribute("totalMoney");//订单金额
		BaseOrder order = new BaseOrder(0, user.getId(), null, money, null, 0, addressId);
		
		//余额支付
		double balance = fundDao.getBalance(user.getId());//账户余额
		if(balance>money){
			int n = fundDao.consume(user.getId(), money, balance-money);//添加消费记录
			if(n>0){
				order.setStatus(2);
			}else{
				order.setStatus(1);
			}
		}else{
			order.setStatus(1);
		}
		//1、添加订单信息
		int n = dao.add(order);
		//获取新订单id
		int baseOrderId = dao.getId(user.getId());
		//2、添加订单详情
		//获取订单选中的商品
		List<CartVo> cartList = (List<CartVo>) session.getAttribute("cartList");
		
		for(CartVo cart:cartList){
			OrderInfo info = new OrderInfo(0, baseOrderId, cart.getGoodsId(), cart.getNum(), cart.getPrice()*cart.getNum());
			orderInfoDao.add(info);
			//3、删除购物车中对应的商品
			cartDao.del(cart.getId());
			//5、更新库存
			GoodsDao goodsDao = new GoodsDaoImpl();
			goodsDao.updateStock(cart.getGoodsId(), cart.getNum());
		}
		//4、刷新购物车商品
		int category = cartDao.getCount(user.getId());//获得购物车中商品种类数量
		List<CartVo> cartVoList = cartDao.findCartVo(user.getId());//获得用户购物商品信息
		double totalMoney = 0;
		for(CartVo vo:cartVoList){
			totalMoney+=vo.getPrice()*vo.getNum();
		}
		session.setAttribute("cartVoList", cartVoList);//购物车列表
		session.setAttribute("category", category);//种类数量
		session.setAttribute("totalMoney", totalMoney);//总价
		
		
		
		if(order.getStatus()==1){
			response.sendRedirect("cartfour.jsp");
			return;
		}
		response.sendRedirect("cartthree.jsp");
	}
	
	public void findByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		List<BaseOrder> list = dao.findByUserId(user.getId());
		System.out.println(list);
		session.setAttribute("orderList", list);
		response.sendRedirect("order.jsp");
	}
	
	//1、修改订单状态；2、回退库存
	public void cancel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
		GoodsDao goodsDao = new GoodsDaoImpl();
		//修改订单状态
		int id = Integer.parseInt(request.getParameter("id"));
		int n = dao.cancel(id);
		//退回库存
		List<OrderVo> orderInfoList = orderInfoDao.findOrder(id);
		for(OrderVo vo:orderInfoList){
			goodsDao.addStock(vo.getGoodsId(), vo.getBuyNum());
		}
		
		if(n>0){
			findByUserId(request,response);
		}
	}
	
	public void confirm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		int n = dao.confirm(id);
		if(n>0){
			findByUserId(request,response);
		}
	}
}
